//
//  MineView.m
//  iMine
//
//  Created by gaoxiang on 09-3-7.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "MineView.h"


@implementation MineView

@synthesize viewImage;

- (id)initWithFrame:(NSRect)frame
{
    if (![super initWithFrame:frame])
        return nil;
	
    return self;
}

- (BOOL)mouseDownCanMoveWindow
{
	return NO;
}

// randomPoint returns a random point inside the view
- (NSPoint)randomPoint
{
    NSPoint result;
    NSRect r = [self bounds];
    result.x = r.origin.x + random() % (int)r.size.width;
    result.y = r.origin.y + random() % (int)r.size.height;
    return result;
}

- (void)mouseDown:(NSEvent *)theEvent
{
	[self postMouseDownNotification:theEvent];
}
- (void)rightMouseDown:(NSEvent *)theEvent
{
	[self postRightMouseDownNotification:theEvent];
}

- (void)mouseDragged:(NSEvent *)theEvent
{
	[self postMouseDraggedNotification:theEvent];
}
- (void)mouseUp:(NSEvent *)theEvent
{
	[self postMouseUpNotification:theEvent];
}
- (void)otherMouseDown:(NSEvent *)theEvent
{
	[self postOtherMouseDownNotification:theEvent];
}
- (void)otherMouseUp:(NSEvent *)theEvent
{
	[self postOtherMouseUpNotification:theEvent];
}
- (void)otherMouseDragged:(NSEvent *)theEvent
{
	[self postOtherMouseDraggedNotification:theEvent];
}
- (void)drawRect:(NSRect)rect {
	if (viewImage)
	{
		NSRect imageRect;
		imageRect.origin = NSZeroPoint;
		imageRect.size = [viewImage size];
		[viewImage drawInRect:imageRect fromRect:imageRect operation:NSCompositeSourceOver fraction:1];
	}
}

- (void)postMouseDownNotification:(NSEvent *)theEvent {
	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
	[nc postNotificationName:@"MineViewMouseDown" object:theEvent];
}
- (void)postRightMouseDownNotification:(NSEvent *)theEvent {
	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
	[nc postNotificationName:@"MineViewRightMouseDown" object:theEvent];
}
- (void)postMouseUpNotification:(NSEvent *)theEvent {
	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
	[nc postNotificationName:@"MineViewMouseUp" object:theEvent];
}

- (void)postOtherMouseDownNotification:(NSEvent *)theEvent {
	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
	[nc postNotificationName:@"MineViewOtherMouseDown" object:theEvent];
}
- (void)postOtherMouseUpNotification:(NSEvent *)theEvent {
	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
	[nc postNotificationName:@"MineViewOtherMouseUp" object:theEvent];
}
- (void)postOtherMouseDraggedNotification:(NSEvent *)theEvent {
	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
	[nc postNotificationName:@"MineViewOtherMouseDragged" object:theEvent];
}

- (void)postMouseDraggedNotification:(NSEvent *)theEvent {
	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
	[nc postNotificationName:@"MineViewMouseDragged" object:theEvent];
}

- (void)resizeView:(NSSize)size
{
	[[self window] setFrame:NSMakeRect([[self window] frame].origin.x, [[self window] frame].origin.y, [viewImage size].width + 40, [viewImage size].height + 95) display:YES animate:YES];

	[[self animator] setFrame:NSMakeRect(20, 20, [viewImage size].width, [viewImage size].height)];
	
}

- (void)dealloc
{
	[super dealloc];
}

@end
